<?php

namespace app\home\controller;

use think\Db;
use think\Session;

class Flow extends \think\Controller {

    public function flowAdd() {
        // 判断用户是否登录
        $c_user_id = Session::get('id');
        if (!$c_user_id) {
            $this->redirect('user/login');
        }
        // 判断是否获取数据
        $data = input('post.');
        if ($data) {
            $id = $data['c_goods_id'];
            // 查询库存
            $info = Db::name('goods')->find($id);
            if ($data['c_goods_number'] > $info['goods_number']) {
                $data['c_goods_number'] = $info['goods_number'];
            }
            //判断购物车里是否有该商品, 购物车状态 0 代付款
            $cat = Db::name('cat')->where('c_goods_id',$id)->where('c_status',0)->where('c_user_id',$c_user_id)->find();
            if($cat){
                $cat['c_goods_number'] += $data['c_goods_number'];
                //判断商品总量是否大于库存
                 if ($cat['c_goods_number'] > $info['goods_number']) {
                     $cat['c_goods_number'] = $info['goods_number'];
                  }
                $cat['c_update_time'] = date('Y-m-d H:i:s');
                $add = Db::name('cat')->update($cat);
                 //判断是否更新成功
                 if($add){
                 $this->success('添加购物车成功!', 'goods/goodsList');
                 }else{
                    $this->error('添加购物车失败!', 'index/index');
                 }
            }else{
                $data['c_user_id'] = $c_user_id;
                $data['c_goods_name'] = $info['goods_name'];
                $data['c_goods_price'] = $info['goods_price'];
                $data['c_goods_img'] = $info['goods_big_img'];
                $data['c_create_time'] = date('Y-m-d H:i:s');
                $data['c_price'] = $data['c_goods_number'] * $info['goods_price'];
                $info = Db::name('cat')->insert($data);
                if ($info) {
                  $this->success('添加购物车成功!', 'goods/goodsList');
                  } else {
                  $this->error('添加购物车失败!', 'index/index');
                 }  
            }
            
            
        }
    }

    public function flowList() {
        $c_user_id = Session::get('id');
        if (!$c_user_id) {
            $this->redirect('user/login');
        }
        

        $data = Db::name('cat')->where('c_status', 0)->where('c_user_id',$c_user_id)->select();
        if ($data) {
            $allprice = Db::name('cat')->where('c_status', 0)->where('c_user_id',$c_user_id)->sum('c_price');
            $this->assign('allprice', $allprice);
            $this->assign('data', $data);
            return $this->fetch('flowList');
        } else {
           return $this->success('购物车空空如也,先去购物吧!','goods/goodsList');
        }

    }

    public function jiesuan() {
        $c_user_id = Session::get('id');
        if (!$c_user_id) {
            $this->redirect('user/login');
        }
        $data = Db::name('cat')->alias('c')
                              ->join('t_goods g','c.c_goods_id = g.goods_id')
                              ->where('c.c_status', 0)
                              ->where('c.c_user_id',$c_user_id)
                              ->field('c.*,g.goods_id,g.goods_number')
                              ->select();
//       dump($data);exit; 
        if ($data) {
             $allprice = Db::name('cat')->where('c_status', 0)->where('c_user_id',$c_user_id)->sum('c_price');
            $number = Db::name('cat')->where('c_status', 0)->where('c_user_id',$c_user_id)->sum('c_goods_number');
            $this->assign('allprice', $allprice);            
            $this->assign('number', $number);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $this->redirect('flowList');
        }
    }

    public function tijiao() {
        return $this->fetch('tijiao');
    }

    public function reduce() {
        $id = input('param.c_id');
        $number = input('param.c_goods_number');
        $number = $number - 1;
        if ($number >= 1) {

            $info = Db::name('cat')->find($id);
            $data = [
                'c_update_time' => date('Y-m-d H:i:s'),
                'c_id' => $id,
                'c_goods_number' => $number,
                'c_price' => $info['c_goods_price'] * $number
            ];
            Db::name('cat')->update($data);
        }
    }

    public function add() {
        $id = input('param.c_id');
        $number = input('param.c_goods_number');
        $number = $number + 1;
        $info = Db::name('cat')->find($id);
        $goods_id = $info['c_goods_id'];
        $goods_number = Db::name('goods')->where('goods_id', $goods_id)->column('goods_number');
        if ($number > $goods_number[0]) {
            $number = $goods_number[0];
            echo $number;
        }
        $data = [
            'c_update_time' => date('Y-m-d H:i:s'),
            'c_id' => $id,
            'c_goods_number' => $number,
            'c_price' => $info['c_goods_price'] * $number
        ];
        Db::name('cat')->update($data);
    }

    public function change() {
        $id = input('param.c_id');
        $number = input('param.c_goods_number');
        $info = Db::name('cat')->find($id);
        $goods_id = $info['c_goods_id'];
        $goods_number = Db::name('goods')->where('goods_id', $goods_id)->column('goods_number');
        if ($number > $goods_number[0]) {
            $number = $goods_number[0];
            echo $number;
        }
        $data = [
            'c_update_time' => date('Y-m-d H:i:s'),
            'c_id' => $id,
            'c_goods_number' => $number,
            'c_price' => $info['c_goods_price'] * $number
        ];
        Db::name('cat')->update($data);
    }
  public function del(){
       $id = input('param.id');
        if($id){
           Db::name('cat')->delete($id);
           $this->redirect('flowList');     
         }
  }  
       
}
